package kmeans.documenti;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;


public class LettoreFile {
	Map<String, List<Documento>> cat_docs;
	List<String> wordList;
	List<Documento> docs;
	private String file;
	private String percorso;
	
	public LettoreFile(String nomefile, String percorso){
		this.file = nomefile;
		this.percorso = percorso;
		this.cat_docs = new HashMap<String,List<Documento>>();
		this.wordList = new Vector<String>();
		this.docs = new LinkedList<Documento>();

	}
	
	
	public void writeToFile(String filename, String s) {
        BufferedWriter bufferedWriter = null; 
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(this.percorso+filename));
            bufferedWriter.write(s);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedWriter
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
	
	private String leggiRiga(BufferedReader reader){
		String riga = null;
		try {
			riga = reader.readLine();
			return riga;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return riga;
	}
	public void Lettore() throws FileNotFoundException{
		BufferedReader reader = new BufferedReader(new FileReader(this.file));
		String categoria = "", doc= "";
		String linea = this.leggiRiga(reader);
		int j,i=0;
		if(linea!=null){
			i++;}
		do{
			for(j=0; j<linea.length(); j++){
				if(linea.charAt(j)=='\t'){
					categoria = linea.substring(0,j);
					doc = linea.substring(j+1, linea.length());
					Documento documento = new Documento(categoria,doc,null,0);
					this.docs.add(documento);
					this.writeToFile("doc"+i,doc);
					if(!this.cat_docs.containsKey(categoria)){
						List<Documento> l = new LinkedList<Documento>();
						l.add(documento);
						this.cat_docs.put(categoria, l);
					}else{
					this.cat_docs.get(categoria).add(documento);}				
				}
			
			}
			
			linea = this.leggiRiga(reader);
			if(linea!=null){	
				i++;
			}
		} while(linea!= null);
	}
	
	public void removeFile(String percorso){
		File path = new File(percorso);
		for ( File file : path.listFiles() ){
			if(file.isFile()){
				file.delete();
			}
		}		
	}
	public Map<String, List<Documento>> getCat_docs() {
		return cat_docs;
	}
	public void setCat_docs(Map<String, List<Documento>> cat_docs) {
		this.cat_docs = cat_docs;
	}
	public List<String> getWordList() {
		return wordList;
	}
	public void setWordList(List<String> wordList) {
		this.wordList = wordList;
	}
	public List<Documento> getDocs() {
		return docs;
	}
	public void setDocs(List<Documento> docs) {
		this.docs = docs;
	}

}
